
class Base64 {
    // private property 
    _keyStr
    mapEn
    mapDe
    _keyStr1
    mapEn1
    mapDe1
    _keyStr2
    mapEn2
    mapDe2
    constructor() {
        this._keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz+/=";
        this.mapEn = this._keyStr.split('');
        this.mapDe = {};
        for (let i = 0; i < this.mapEn.length; i++) {
            this.mapDe[this.mapEn[i]] = i;
        }

        this._keyStr1 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/=";
        this.mapEn1 = this._keyStr1.split('');
        this.mapDe1 = {};
        for (let i = 0; i < this.mapEn1.length; i++) {
            this.mapDe1[this.mapEn1[i]] = i;
        }

        this._keyStr2 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=";
        this.mapEn2 = this._keyStr2.split('');
        this.mapDe2 = {};
        for (let i = 0; i < this.mapEn2.length; i++) {
            this.mapDe2[this.mapEn2[i]] = i;
        }
    }
    // public method for encoding 
    encode = (input, type?) => {
        let output = "";
        let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        let i = 0;
        input = this._utf8_encode(input);
        let mapEn = this.mapEn
        if (type == 1) {
            mapEn = this.mapEn1
        } else if (type == 2){
            mapEn = this.mapEn2
        }
        while (i < input.length) {
            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);
            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;
            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }
            output = output + mapEn[enc1] + mapEn[enc2] + mapEn[enc3] + mapEn[enc4];
        }
        return output;
    }

    // public method for decoding 
    decode = (input, type?) => {
        let output = "";
        let chr1, chr2, chr3;
        let enc1, enc2, enc3, enc4;
        let i = 0;
        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
        let str = [...input]
        let mapDe = this.mapDe
        if (type == 1) {
            mapDe = this.mapDe1
        } else if (type == 2){
            mapDe = this.mapDe2
        }
        while (i < input.length) {
            enc1 = mapDe[str[i++]];
            enc2 = mapDe[str[i++]];
            enc3 = mapDe[str[i++]];
            enc4 = mapDe[str[i++]];
            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;
            output = output + String.fromCharCode(chr1);
            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }
        }
        output = this._utf8_decode(output);
        return output;
    }

    // private method for UTF-8 encoding 
    _utf8_encode = (string) => {
        string = string.replace(/\r\n/g, "\n");
        let utftext = "";
        for (let n = 0; n < string.length; n++) {
            let c = string.charCodeAt(n);
            if (c < 128) {
                utftext += String.fromCharCode(c);
            } else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            } else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
        }
        return utftext;
    }

    // private method for UTF-8 decoding 
    _utf8_decode = (utftext) => {
        let string = "";
        let i = 0;
        let c = 0, c1 = 0, c2 = 0, c3;
        while (i < utftext.length) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            } else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            } else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }

}


export let base64 = new Base64();

class StrEnco {
    key
    keyArray
    keyObj
    constructor() {
        this.key = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        this.keyArray = this.key.split("")
        this.keyObj = {}
        for (let i = 0; i < this.keyArray.length; i++) {
            this.keyObj[this.keyArray[i]] = i
        }
    }
    encode = function (str) {  //加密字符串
        //定义密钥，36个字母和数字
        let len = this.key.length;  //获取密钥的长度
        let a = this.keyArray;  //把密钥字符串转换为字符数组
        let s = "", b, b1, b2, b3;  //定义临时变量
        for (let i = 0; i < str.length; i++) {  //遍历字符串
            b = str.charCodeAt(i);  //逐个提取每个字符，并获取Unicode编码值
            b1 = b % len;  //求Unicode编码值得余数
            b = (b - b1) / len;  //求最大倍数
            b2 = b % len;  //求最大倍数的于是
            b = (b - b2) / len;  //求最大倍数
            b3 = b % len;  //求最大倍数的余数
            s += a[b3] + a[b2] + a[b1];  //根据余数值映射到密钥中对应下标位置的字符
        }
        return s;  //返回这些映射的字符
    }
    decode = function (str) {
        //定义密钥，36个字母和数字
        let len = this.key.length;  //获取密钥的长度
        let b, b1, b2, b3, d = 0, s;  //定义临时变量
        s = new Array(Math.floor(str.length / 3));  //计算加密字符串包含的字符数，并定义数组
        b = s.length;  //获取数组的长度
        for (let i = 0; i < b; i++) {  //以数组的长度循环次数，遍历加密字符串
            b1 = this.keyObj[str.charAt(d)];  //截取周期内第一个字符串，计算在密钥中的下标值
            d++;
            b2 = this.keyObj[str.charAt(d)];  //截取周期内第二个字符串，计算在密钥中的下标值
            d++;
            b3 = this.keyObj[str.charAt(d)];  //截取周期内第三个字符串，计算在密钥中的下标值
            d++;
            s[i] = b1 * len * len + b2 * len + b3  //利用下标值，反推被加密字符的Unicode编码值
        }
        b = eval("String.fromCharCode(" + s.join(',') + ")"); //用fromCharCode()算出字符串
        return b;  //返回被解密的字符串
    }
}




export let strEnco = new StrEnco();

let encoList = {
    0: 'a',
    1: 'b',
    2: 'c',
    3: 'd',
    4: 'e',
    5: 'f',
    6: 'g',
    7: 'h',
    8: 'i',
    9: 'j',
    a: 'k',
    b: 'l',
    c: 'm',
    d: 'n',
    e: 'o',
    f: 'p',
    g: 'q',
    h: 'r',
    i: 's',
    j: 't',
    k: 'u',
    l: 'v',
    n: 'w',
    m: 'x',
    o: 'y',
    p: 'z',
    q: 'A',
    r: 'B',
    s: 'C',
    t: 'D',
    u: 'E',
    v: 'F',
    w: 'G',
    x: 'H',
    y: 'I',
    z: 'J',
    A: 'K',
    B: 'L',
    C: 'N',
    D: 'M',
    E: 'O',
    F: 'P',
    G: 'Q',
    H: 'R',
    I: 'S',
    J: 'T',
    K: 'U',
    L: 'V',
    N: 'W',
    M: 'X',
    O: ':',
    P: '"',
    Q: '0',
    R: '1',
    S: '2',
    T: '3',
    U: '4',
    V: '5',
    W: '6',
    X: '7',
    Y: '8',
    Z: '9',
    '{': '{',
    '}': '}',
    ':': 'Y',
    '[': '[',
    ']': ']',
    ',': ',',
    '"': 'Z',
    '!': '+',
    '@': '_',
    '#': '=',
    '$': '-',
    '%': '%',
    '^': '^',
    '&': '&',
    '*': '*',
    '(': '(',
    ')': ')',
    '-': '$',
    '=': '#',
    '_': '@',
    '+': '!',
    '.': '.',
}

let decoList = {}
Object.keys(encoList).forEach((key) => {
    decoList[encoList[key]] = key
})

let mapEn = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=abcdefghijklmnopqrstuvwxyz".split('');
let mapDe = {};
for (let i = 0; i < mapEn.length; i++) {
    mapDe[mapEn[i]] = i;
}

class StrEnco2 {
    _keyStr: string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=abcdefghijklmnopqrstuvwxyz"
    encode(str: string): string {
        let strArray = [...str]
        for (let i = 0; i < strArray.length; i++) {
            let tmp = strArray[i];
            let value = encoList[tmp]
            if (value) {
                strArray[i] = value
            }
        }
        return strArray.join('');
    }
    decode(str: string): string {
        let strArray = [...str]
        for (let i = 0; i < str.length; i++) {
            let tmp = strArray[i];
            let value = decoList[tmp]
            if (value) {
                strArray[i] = value
            }
        }
        return strArray.join('');
    }
}
export let strEnco2 = new StrEnco2()